<template>
  <div class="topbar">
    <div class="wrap tapbarbox">
      <div>欢迎来到叩丁狼积分商城</div>
      <ul>
        <li class="last">
          <img src="../assets/img/userImg.f8bbec5e.png" alt="" />
          用户名:游客
        </li>
        <li>我的积分:--</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="first" @click="changeIsLoginShow(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>
                    
<script>
import { weiXinLoginAPI } from "@/request/api";
import { mapMutations, mapState, mapActions } from "vuex";
export default {
  name: "Emoo2Topbar",
  data() {
    return {
      cartTotal: 5,
    };
  },
  methods: {
    ...mapMutations({
      changeIsLoginShow: "showModal/changeIsLoginShow",
      loginStatus: "loginStatus/loginStatus",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
    }),
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
    }),
  },
  async created() {
    let code = this.$route.query.code;
    if (code) {
      let res = await weiXinLoginAPI({
        code,
      });
      if (res.data.code == 0) {
        this.asyncChangeIsShowToast({
          isshow: true,
          msg: "登录成功",
          type: "success",
        });
        localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
        this.loginStatus(true);
        this.$route.query.code && this.$router.push(this.$router.path);
      } else if (res.code == 400) {
        this.asyncChangeIsShowToast({
          isshow: true,
          msg: "二维码已失效，请重新打开",
          type: "warning",
        });
        this.changeIsLoginShow(true);
      } else if (res.code == 407) {
        this.asyncChangeIsShowToast({
          isshow: true,
          msg: "请使用手机号再进行登录",
          type: "warning",
        });
        this.changeIsLoginShow(true);
        localStorage.setItem("uuid", res.data.uuid);
      } else {
        let token = localStorage.getItem("x-auth-token");
        this.loginStatus(Boolean(token));
      }
    }
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background-color: #242b39;
  color: #fffefe;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  .tapbarbox {
    display: flex;
    justify-content: space-between;
    line-height: 40px;
    ul {
      display: flex;
      justify-content: space-between;
      li {
        cursor: pointer;
        line-height: 40px;
        margin-left: 20px;
        img {
          width: 26px;
          height: 26px;
          background: #7d8696;
          border: 1px solid #8891a1;
          border-radius: 13px;
          align-items: center;
        }
      }
      .last {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      .first {
        background-color: #0a328e;
        width: 124px;
        height: 40px;
        text-align: center;
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
      &.cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>